<?php
/**
 * Flea Site
 *
 * @copyright  Copyright (c) 2008 Movoin Studio (http://movoin.com)
 * @license    Apache License 2.0
 * @version    $Id: User.php 138 2009-05-25 06:42:38Z movoin $
 */

/**
 * 用户模型
 * @todo 区分前后台用户（isAdmin)
 *
 */
class Model_User
{

    var $tblUser;

    var $tblUserGroup;

    function Model_User()
    {
        $this->tblUser = & FLEA::getSingleton( 'Table_User' );
        $this->tblUserGroup = & FLEA::getSingleton( 'Table_Usergroup' );
    }

    function getUser($userId, $groupId=null)
    {
        if($groupId) {
            $where[] = array('group_id', (int)$groupId);
        }
        if($userId) {
            $where[] = array('user_id', (int)$userId);
        }
        return $this->tblUser->find($where, 'group_id ASC, user_id DESC');
    }

    function getAllUsers($groupId=null)
    {
        if($groupId) {
            $where[] = array('group_id', (int)$groupId);
        }
        //开启用户组关联
        $this->tblUser->enableLink('group');
        return $this->tblUser->findAll($where, 'group_id ASC, user_id DESC', null, 'user_id, username, islocked, created');
    }

    function saveUser(&$data)
    {
        if(!$data['username'])
            return '用户名未填写!';
        if(!$data['email'])
            return '用户邮箱未填写!';
        if($data['user_id']) {
            if(!$data['password']) {
                unset($data['password']);
            } else {
                $data['password'] = md5($data['password']);
            }
        } else {
            if(!$data['password']) {
                return '用户密码必须填写!';
            } else {
                $data['password'] = md5($data['password']);
            }
        }

        if($this->tblUser->save($data)) {
            return '保存成功!';
        }
        return '保存失败!';
    }

    function removeUser($userid)
    {
        if( $this->tblUser->removeByPkv($userid) ) {
            return '删除成功！';
        }
        return '删除失败！';
    }

    function getGroup($groupId, $fields='group_id, name, description, options')
    {
        return $this->tblUserGroup->find($groupId, null, $fields);
    }

    function getAllGroups($site_id, $fields = 'group_id, name, description, options,created')
    {
        $where = null;
        if($site_id) {
            $where = array(
                array('site_id',$site_id)
            );
        }
        return $this->tblUserGroup->findAll($where, 'created ASC', null, $fields);
    }

    function saveGroup(&$data)
    {
        if(!$data['name']) {
            return '名称未填写！';
        }
        if($this->tblUserGroup->save($data)) {
            return '保存成功！';
        }
        return '保存失败！';
    }

    function removeGroup($groupid)
    {
        if( $this->tblUserGroup->removeByPkv($groupid) ) {
            return '删除成功！';
        }
        return '删除失败！';
    }

    /**
     * 返回表实例
     *
     */
    function &getTable($type='user')
    {
        if($type=='user') {
            return $this->tblUser;
        }
        return $this->tblUserGroup;
    }
}
